using RaceHFTools.BluetoothLE.Bean;
using RaceHFTools.BluetoothLE.Bean.Module;
using System;
using System.Threading.Tasks;

using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace RaceHFTools.Pages.Tweak
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class TweakRecordPage : ContentPage
    {
        private readonly Bean bean;

        public TweakRecordPage(Bean bean)
        {
            InitializeComponent();

            this.bean = bean;
        }

        #region 事件处理
        bool type_set_done, trig_set_done;

        void OnAllSetDone()
        {
            if (type_set_done && trig_set_done)
            {
                Device.BeginInvokeOnMainThread(async () => await DisplayAlert("文件记录设置", "设置完毕", "确定"));
            }
        }

        void ResetAllFlags()
        {
            type_set_done = false;
            trig_set_done = false;
        }

        void OnTrigGet(TweakRecordTrigType type)
        {
            Device.BeginInvokeOnMainThread(() =>
            {
                switch (type)
                {
                    case TweakRecordTrigType.ByGnss:
                        cbxRecordTrigGnss.IsChecked = true;
                        break;

                    case TweakRecordTrigType.BySpeed:
                        cbxRecordTrigSpeed.IsChecked = true;
                        break;

                    default:
                        break;
                }
            });
        }

        void OnTypeGet(TweakRecordFileType type)
        {
            Device.BeginInvokeOnMainThread(() =>
            {
                if (type.nma) cbxRecordNma.IsChecked = true;
                if (type.acc) cbxRecordAcc.IsChecked = true;
                if (type.rhf) cbxRecordRhf.IsChecked = true;
                if (type.vbo) cbxRecordVbo.IsChecked = true;
            });
        }

        void OnTrigSet(bool success)
        {
            type_set_done = success;
            OnAllSetDone();
        }

        void OnTypeSet(bool success)
        {
            trig_set_done = success;
            OnAllSetDone();
        }

        async Task<bool> DoTrigSet()
        {
            bool bygps = cbxRecordTrigGnss.IsChecked;
            bool byspeed = cbxRecordTrigSpeed.IsChecked;

            if (!bygps && !byspeed)
            {
                Device.BeginInvokeOnMainThread(async () => await DisplayAlert("文件记录", "至少选择一种触发方式", "确定"));
                return false;
            }

            TweakRecordTrigType type = TweakRecordTrigType.ByGnss;
            if (bygps) type = TweakRecordTrigType.ByGnss;
            else if (byspeed) type = TweakRecordTrigType.BySpeed;

            await bean.Tweak.SetRecordTrigType(type);

            return true;
        }

        async Task<bool> DoTypeSet()
        {
            bool nma = cbxRecordNma.IsChecked;
            bool acc = cbxRecordAcc.IsChecked;
            bool rhf = cbxRecordRhf.IsChecked;
            bool vbo = cbxRecordVbo.IsChecked;

            TweakRecordFileType type;
            type.nma = nma;
            type.acc = acc;
            type.rhf = rhf;
            type.vbo = vbo;

            await bean.Tweak.SetRecordFileType(type);

            return true;
        }
        #endregion

        #region OnAppearing/OnDisappearing
        protected override void OnAppearing()
        {
            base.OnAppearing();

            bean.Tweak.RecordGetTrigEvent += OnTrigGet;
            bean.Tweak.RecordGetFileEvent += OnTypeGet;
            bean.Tweak.RecordSetTrigEvent += OnTrigSet;
            bean.Tweak.RecordSetFileEvent += OnTypeSet;

            _ = bean.Tweak.GetRecordAll();
        }

        protected override void OnDisappearing()
        {
            bean.Tweak.RecordGetTrigEvent -= OnTrigGet;
            bean.Tweak.RecordGetFileEvent -= OnTypeGet;
            bean.Tweak.RecordSetTrigEvent -= OnTrigSet;
            bean.Tweak.RecordSetFileEvent -= OnTypeSet;

            base.OnDisappearing();
        }
        #endregion

        #region 交互事件
        void CheckBox_RecordTrig_CheckedChanged(object sender, CheckedChangedEventArgs e)
        {
            if (e.Value == false)
                return;

            CheckBox cbx = sender as CheckBox;
            CheckBox[] cbxs = new CheckBox[] { cbxRecordTrigGnss, cbxRecordTrigSpeed };
            foreach (CheckBox c in cbxs)
            {
                c.IsChecked = c == cbx;
            }
        }

        async void Button_RecordApply_Clicked(object sender, EventArgs e)
        {
            ResetAllFlags();

            await DoTrigSet();
            await DoTypeSet();
        }
        #endregion
    }
}

